/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function initFileGrid(){
    $("#files_grid").jqGrid({
        url:'getDocumentosList.do',
        datatype: "json",
        width:600,
 colNames:['','filename',''],
        colModel:[
            {
            name:'actions',
            index:'actions',
            sortable: false, 
            width:5, 
            
            align:"center"
        } ,
        {
            name:'fileid',
            index:'fileid',
            sortable: true, 
            hidden: true , 
            editable: true, 
            width:55
        }
        ,{
            name:'filename',
            index:'filename',
            sortable: true, 
            editable: true,  
            editrules: { required: true},
            formatter:'showlink',
            formatoptions:{baseLinkUrl:'getDocsDownload.do', addParam: '&fileid=334'},
            width:55
        }
        ],
        rowNum:10,
        rowList:[10,20,30],
        pager: '#files_pager',
        sortname: 'filename',
        viewrecords: true,
        sortable: true,
        sortorder: "desc",
        multiselect: false,
        caption:"Documentos anexos",
        loadonce : false,
        rowTotal: 'total',
        records:'records',
        jsonReader: {
            repeatitems: false,
            id: "fileid"
        }
        , 
        gridComplete: function(){
 	  
            var ids =$("#files_grid").jqGrid('getDataIDs');
	  
            for(var i=0;i < ids.length;i++){
                var fileid_ = ids[i]; 
               
               // var fileid_=(filename_.substring(filename_.lastIndexOf(".")+1)).toLowerCase();
                var fileName=jQuery('#files_grid').jqGrid ('getCell', fileid_, 'filename');
                var fileExtencion=(fileName.substring(fileName.lastIndexOf(".")+1)).toLowerCase();
                            
                          icon= " <img src='img/icon2.png'alt='Ver auto' title='Aceptar' height='16' onclick=\"deleteFile('"+fileid_+"');\" width='16'  />";
                          icon2= " <img src='img/"+fileExtencion+".png'alt='Ver auto' title='Aceptar' height='16' onclick=\"alert('"+fileExtencion+"');\" width='16'  />";
                       
              
                $("#files_grid").jqGrid('setRowData',ids[i],{
                       actions:icon+icon2
                   
                }); 
            }
 
        }
    });
    $("#files_grid").jqGrid('navGrid','#files_pager',{
        edit:true,
        add:true,
        del:true,
        view :false,
        search: false 
    },{closeAfterEdit:true,url:'adcBanco.do?action=edit'},{closeAfterAdd:true,url:'saveBanco.do?action=add'},{url:'adcBanco.do?action=del'});

}


function deleteFile(fileid){
   // alert(fileid)
    dwrService.deleteFile(fileid,function(data)
                        {
                              /* dwr.util.setValue('detail', data,{
                                    escapeHtml:false
                                    
                               });*/
        
                                    $("#files_grid").setGridParam().trigger("reloadGrid");
                   });
}